package com.axon.components.controls
{
	import com.axon.components.containers.AxPanel;
	import com.axon.components.containers.AxTitleWindowSizeable;
	
	import flash.display.DisplayObjectContainer;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.ui.Keyboard;
	
	import mx.controls.TextArea;
	import mx.core.Container;
	import mx.events.FlexEvent;

	[Style(name="axBackgroundColor",			type="uint", format="Color", inherit="yes")]
	[Style(name="axBackgroundColorReadOnly", 	type="uint", format="Color", inherit="yes")]

	public class AxTextArea extends TextArea
	{
		//////////////////////////////////////////////////////////////////////////////////////////////////////
		
		public function AxTextArea()
		{
			super();

			addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
			addEventListener(FlexEvent.CREATION_COMPLETE, onCreationComplete);
		}
		
		//////////////////////////////////////////////////////////////////////////////////////////////////////
		
		override public function set editable(value: Boolean): void
		{
			super.editable = value;
			refreshStyle();
		}

		//////////////////////////////////////////////////////////////////////////////////////////////////////
		
		protected function onCreationComplete( event: FlexEvent ): void
		{
			refreshStyle();
		}
		
		//////////////////////////////////////////////////////////////////////////////////////////////////////
		
		protected function onKeyDown( event: KeyboardEvent ): void
		{
			var theParent: DisplayObjectContainer = parent;
					
    		switch (event.charCode) {
    			
    			case Keyboard.ENTER:
    			
    				if ( event.ctrlKey ) {

						while ((theParent != null) && !(theParent is AxPanel) && !(theParent is AxTitleWindowSizeable))
							theParent = theParent.parent;
							
						if ( (theParent != null)&& (Container(theParent).defaultButton) )
							Container(theParent).defaultButton.dispatchEvent(new MouseEvent(MouseEvent.CLICK)); }
					
					break;
				
				case Keyboard.ESCAPE:
				
					while ((theParent != null) && !(theParent is AxPanel) && !(theParent is AxTitleWindowSizeable))
						theParent = theParent.parent;
						
					if (theParent != null) 
						theParent.dispatchEvent(event.clone());
						
					break;
					
				default:
    		
    		}
		}

		//////////////////////////////////////////////////////////////////////////////////////////////////////

		protected function refreshStyle(): void
		{
			setStyle("backgroundColor", (editable) ? getStyle("axBackgroundColor") : getStyle("axBackgroundColorReadOnly"));
		}		

	}
}